//
//  MapScreen.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/6.
//
//

#include "MapScreen.h"
#include "MapMenuItem.h"
#include "PopupLayer.h"
#include "ShowWinScreen.h"
#include "LevelScreen.h"
#include "MapConfig.h"
#include "MapStarView.h"
#include "PlayLevelRecorder.h"
#include "DebugConfig.h"
#include "CountDownModeLevelScreen.h"

#define  MAX_LEVEL_TAG    71

#define  EXTEND_LEVEL_BASE 1000
#define  EXTEND_LEVEL_NUM 10

#define  MAP_SCREEN_WIDTH  (320)
#define  MAP_SCREEN_HEIGHT (568)
#define  MAP_CHAPTER_NUM   (2)
#define  MAP_TOP_PAD       (100)

typedef enum {
    ZBg,
    ZLevelMenu,
    ZStar,
    ZMenu,
    


} ZIndex;

MapScreen::MapScreen()
{
}


Scene *MapScreen::createScene()
{
    auto scene = Scene::create();
    auto layer = MapScreen::create();
    scene->addChild(layer);
    return scene;
}

bool MapScreen::init()
{
    if (!Layer::init()) {
        return false;
    }
    
    bgInit();
    menuInit();
    extendMenuInit();
    optionMenuViewInit();
    upMenuInit();
    goldMenuInit();
    
    
    // bind touch event
    auto touchListener = EventListenerTouchOneByOne::create();
    touchListener->onTouchBegan = CC_CALLBACK_2(MapScreen::onTouchBegan, this);
    touchListener->onTouchMoved = CC_CALLBACK_2(MapScreen::onTouchMoved, this);
    touchListener->onTouchEnded = CC_CALLBACK_2(MapScreen::onTouchEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

    return true;
}

void MapScreen::onEnter()
{
	int count = mapBollMenu->getChildrenCount();
	for (int i = 1; i < count; i++) {
		auto item = mapBollMenu->getChildByTag(i);
		MapMenuItem *menuItem = (MapMenuItem*)item;

		if(DebugConfig::isDebugModel()){//gaoyu add debug model for test
		    menuItem->SetNum(i);
		    menuItem->setEnabled(true);
		}else{
			if(i == 1 || PlayLevelRecorder::getInstance()->readLevelPassInfo(i) == 1||
			  (i > 1 && PlayLevelRecorder::getInstance()->readLevelPassInfo(i - 1) == 1)){
				 menuItem->SetNum(i);
				 menuItem->setEnabled(true);
			}else{
			     menuItem->setEnabled(false);
			}
		}


        if(PlayLevelRecorder::getInstance()->readLevelPassInfo(i) == 1){
        	auto item = this->getChildByTag(MAX_LEVEL_TAG + i);
        	MapStarView *staritem = (MapStarView*)item;
        	int starNum = PlayLevelRecorder::getInstance()->readLevelStarNumInfo(i);
        	if(staritem == nullptr){
    			MapStarView *star = MapStarView::create(starNum);
    			star->setPosition(menuItem->getPosition().x-17,menuItem->getPosition().y-25);
    			this->addChild(star, ZStar);
        	}else{
        		staritem->setStarNum(starNum);
        	}
        }
	}
	Layer::onEnter();
}

void MapScreen::menuInit()
{
    mapBollMenu = Menu::create();
    mapBollMenu->setAnchorPoint(Vec2::ZERO);
    mapBollMenu->setPosition(Vec2::ZERO);
    mapBollMenu->setTag(0);
    
    for(int i = 1;i<MAX_LEVEL_TAG;i++){
        auto menuItem = MapMenuItem::create("MapBollBlue.png", "MapBollBlueSelected.png","MapBollBlueDisable.png", CC_CALLBACK_1(MapScreen::menuCallback, this));
        menuItem->setPosition(MapConfig::getMapPosition(i));
        menuItem->setTag(i);

        if(DebugConfig::isDebugModel()){//gaoyu add debug model for test
        	menuItem->SetNum(i);
        	menuItem->setEnabled(true);
        }else{
            if(i == 1 || PlayLevelRecorder::getInstance()->readLevelPassInfo(i) == 1 ||
            		(i > 1 && PlayLevelRecorder::getInstance()->readLevelPassInfo(i - 1) == 1)){
            	menuItem->SetNum(i);
            	menuItem->setEnabled(true);
            }else{
            	menuItem->setEnabled(false);
            }
        }
        mapBollMenu->addChild(menuItem);
        
        if(PlayLevelRecorder::getInstance()->readLevelPassInfo(i) == 1){
        	int starNum = PlayLevelRecorder::getInstance()->readLevelStarNumInfo(i);
			MapStarView *star = MapStarView::create(starNum);
			star->setPosition(menuItem->getPosition().x-17,menuItem->getPosition().y-25);
			star->setTag(MAX_LEVEL_TAG + i);
			this->addChild(star, ZStar);
        }
    }
    
    this->addChild(mapBollMenu,ZLevelMenu);
}

void MapScreen::extendMenuInit()
{
	mapExtendMenu = Menu::create();
    mapExtendMenu->setAnchorPoint(Vec2::ZERO);
    mapExtendMenu->setPosition(Vec2::ZERO);
    mapExtendMenu->setTag(1);

    for(int i = 1; i <= EXTEND_LEVEL_NUM; i++){
        auto menuItem = MapMenuItem::create("MapBollRed.png", "MapBollRedSelected.png","MapBollRedDisable.png", CC_CALLBACK_1(MapScreen::extendMenuCallback, this));
        menuItem->setPosition(MapConfig::getMapExtendPosition(EXTEND_LEVEL_BASE + i));
        menuItem->setTag(EXTEND_LEVEL_BASE + i);

        if(DebugConfig::isDebugModel()){//gaoyu add debug model for test
        	menuItem->SetNum(i);
        	menuItem->setEnabled(true);
        }else{
            if(PlayLevelRecorder::getInstance()->readLevelPassInfo(EXTEND_LEVEL_BASE + i) == 1 ||
            		(i > 1 && PlayLevelRecorder::getInstance()->readLevelPassInfo(EXTEND_LEVEL_BASE + i - 1) == 1)){
            	menuItem->SetNum(i);
            	menuItem->setEnabled(true);
            }else{
            	menuItem->setEnabled(false);
            }
        }
        mapExtendMenu->addChild(menuItem);

        if(PlayLevelRecorder::getInstance()->readLevelPassInfo(EXTEND_LEVEL_BASE + i) == 1){
        	int starNum = PlayLevelRecorder::getInstance()->readLevelStarNumInfo(EXTEND_LEVEL_BASE + i);
			MapStarView *star = MapStarView::create(starNum);
			star->setPosition(menuItem->getPosition().x-17,menuItem->getPosition().y-25);
			star->setTag(EXTEND_LEVEL_BASE + EXTEND_LEVEL_NUM + i);
			this->addChild(star, ZStar);
        }
    }

    this->addChild(mapExtendMenu,ZLevelMenu);
}

void MapScreen::menuCallback(Ref *sender)
{
    MenuItemImage *item = (MenuItemImage*)sender;
    doSelectedItem(item->getTag());
}

void MapScreen::doSelectedItem(int tag)
{
    Director::getInstance()->pushScene(LevelScreen::createScene(tag));
}

void MapScreen::extendMenuCallback(Ref *sender)
{
    MenuItemImage *item = (MenuItemImage*)sender;
    doExtendSelectedItem(item->getTag());
}

void MapScreen::doExtendSelectedItem(int tag)
{
    Director::getInstance()->pushScene(CountDownModeLevelScreen::createScene(tag));
}

void MapScreen::bgInit()
{
    Sprite* bg = Sprite::create("mapBg01.png");
    bg->setAnchorPoint(Point(0, 0));
    bg->setPosition(Point(0, 0));
    addChild(bg,ZBg);
    
    Sprite* bg2 = Sprite::create("mapBg02.png");
    bg2->setAnchorPoint(Point(0, 0));
    bg2->setPosition(Point(0, MAP_SCREEN_HEIGHT));
    addChild(bg2,ZBg);
    
    //Sprite* shade = Sprite::create("mapShade.png");
    //shade->setAnchorPoint(Point(0, 0));
    //shade->setPosition(Point(0, MAP_SCREEN_HEIGHT));
    //addChild(shade,ZBg);
}

void MapScreen::optionMenuViewInit()
{
    optionMenuView = OptionMenuView::create();
    optionMenuView->setPosition(Vec2::ZERO);
    addChild(optionMenuView,ZMenu);
}

void MapScreen::upMenuInit()
{
    upMenuView = UpMenuView::create();
    upMenuView->setPosition(Point(UP_MENU_XPAD,UP_MENU_YPAD));
    addChild(upMenuView,ZMenu);
}

void MapScreen::goldMenuInit()
{
	Size size = Director::getInstance()->getWinSize();
	goldView = ShowGoldView::create();
	goldView->setPosition(Point(GOLD_VIEW_LEFT_PAD,size.height - GOLD_VIEW_TOP_PAD));
    addChild(goldView,ZMenu);
}

bool MapScreen::onTouchBegan(Touch *touch, Event *unused)
{
    m_beginPY = touch->getLocation().y;
    return true;
}

void MapScreen::onTouchMoved(Touch *touch, Event *unused)
{
    Size size = Director::getInstance()->getWinSize();
    float newPositionY = this->getPosition().y;
    newPositionY -= m_beginPY - touch->getLocation().y;
    this->setPosition(Point(this->getPosition().x,newPositionY));
    //menus
    optionMenuView->setPosition(Point(this->getPosition().x,-this->getPosition().y));
    upMenuView->setPosition(Point(this->getPosition().x+UP_MENU_XPAD,-this->getPosition().y+UP_MENU_YPAD));
    goldView->setPosition(Point(this->getPosition().x+GOLD_VIEW_LEFT_PAD,-this->getPosition().y+(size.height - GOLD_VIEW_TOP_PAD)));
    //end
    m_beginPY = touch->getLocation().y;
}

void MapScreen::onTouchEnded(Touch *touch, Event *unused)
{
    //CCLog("onTouchEnded %f",this->getPosition().y);
    Size size = Director::getInstance()->getWinSize();
    if(this->getPosition().y > 0){
        this->setPosition(Point(this->getPosition().x,0));
        optionMenuView->setPosition(Vec2::ZERO);
        upMenuView->setPosition(Point(UP_MENU_XPAD,UP_MENU_YPAD));
        goldView->setPosition(Point(GOLD_VIEW_LEFT_PAD,(size.height - GOLD_VIEW_TOP_PAD)));
    
    }
    if(this->getPosition().y < -((MAP_CHAPTER_NUM-1)*MAP_SCREEN_HEIGHT+MAP_TOP_PAD)){
        this->setPosition(Point(this->getPosition().x,-((MAP_CHAPTER_NUM-1)*MAP_SCREEN_HEIGHT+MAP_TOP_PAD)));
        optionMenuView->setPosition(Point(this->getPosition().x,-this->getPosition().y));
        upMenuView->setPosition(Point(this->getPosition().x+UP_MENU_XPAD,-this->getPosition().y+UP_MENU_YPAD));
        goldView->setPosition(Point(this->getPosition().x+GOLD_VIEW_LEFT_PAD,-this->getPosition().y+(size.height - GOLD_VIEW_TOP_PAD)));
    }
    
}








